{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "84bf3140",
   "metadata": {},
   "source": [
    "------------\n",
    "* 2022/10/20/week 09\n",
    "* Python学习记录\n",
    "* 记录人：wu-huichun\n",
    "-----------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "472ef15a",
   "metadata": {},
   "source": [
    "# 项目一"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d5e0068",
   "metadata": {},
   "source": [
    "###  一.初级版\n",
    "\n",
    "> 元音程序的第一个版本显示单词“Milliways”中所有元音（包括重复）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ccf434f1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = \"Milliways\"\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        print(letter)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5119698a",
   "metadata": {},
   "source": [
    "### 二.进阶版\n",
    "\n",
    "> 1.程序增加了一些代码，使用一个列表来避免重复\n",
    "\n",
    "> 2.这个程序会显示单词“Milliways”中找到的唯一元音组成的列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "26044cfc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'e', 'i', 'o', 'u']\n",
      "['a', 'e', 'i', 'o', 'u']\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = \"Milliways\"\n",
    "found = []\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        if letter not in found:\n",
    "            found.append(letter)\n",
    "for vowel in found:\n",
    "    print(vowels)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9faa494c",
   "metadata": {},
   "source": [
    "### 三.高阶版\n",
    "\n",
    "> 从用户输入的一个单词中找到唯一元音"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9cd2e61b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:Hitch-hiker\n",
      "i\n",
      "e\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = []\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        if letter not in found:\n",
    "            found.append(letter)\n",
    "for vowel in found:\n",
    "    print(vowel)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b54880c9",
   "metadata": {},
   "source": [
    "# 项目二"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63d352af",
   "metadata": {},
   "source": [
    "### 一.使用列表方式\n",
    "\n",
    "* 会改变一个列表的状态"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "bb260c3b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)\n",
    "for i in range(4):\n",
    "    plist.pop()\n",
    "plist.pop(0)\n",
    "plist.remove(\"'\")\n",
    "plist.extend([plist.pop(),plist.pop()])\n",
    "plist.insert(2,plist.pop(3))\n",
    "new_phrase = ''.join(plist)\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "074550da",
   "metadata": {},
   "source": [
    "### 二.中括号记法和切片\n",
    "\n",
    "* 不会改变列表状态"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7bb77a84",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)\n",
    "new_phrase = ''.join(plist[1:3])\n",
    "new_phrase = new_phrase + ''.join([plist[5],plist[4],plist[7],plist[6]])\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14179560",
   "metadata": {},
   "source": [
    "# 项目三"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea9ab146",
   "metadata": {},
   "source": [
    "### 一.列表和for循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e81914bc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\t M\n",
      "\t a\n",
      "\t r\n",
      "\t v\n",
      "\t i\n",
      "\t n\n"
     ]
    }
   ],
   "source": [
    "paranoid_android = \"Marvin\"\n",
    "letters = list(paranoid_android)\n",
    "for char in letters:\n",
    "    print('\\t',char)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17c85f89",
   "metadata": {},
   "source": [
    "### 二.中括号记法+三个切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8186cb66",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\t M\n",
      "\t a\n",
      "\t r\n",
      "\t v\n",
      "\t i\n",
      "\t n\n",
      "\n",
      "\t\t A\n",
      "\t\t n\n",
      "\t\t d\n",
      "\t\t r\n",
      "\t\t o\n",
      "\t\t i\n",
      "\t\t d\n",
      "\n",
      "\t\t\t P\n",
      "\t\t\t a\n",
      "\t\t\t r\n",
      "\t\t\t a\n",
      "\t\t\t n\n",
      "\t\t\t i\n",
      "\t\t\t o\n",
      "\t\t\t d\n"
     ]
    }
   ],
   "source": [
    "paranoid_android = \"Marvin, the Paraniod Android\"\n",
    "letters = list(paranoid_android)\n",
    "for char in letters[:6]:\n",
    "    print('\\t',char)\n",
    "print()\n",
    "for char in letters[-7:]:\n",
    "    print('\\t'*2,char)\n",
    "print()\n",
    "for char in letters[12:20]:\n",
    "    print('\\t'*3,char)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42695655",
   "metadata": {},
   "source": [
    "# 四.列表的问题"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe93a26f",
   "metadata": {},
   "source": [
    "### 一.字符串对象列表(适用于少量数据）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "1ff103b4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Ford Perfect', 'Male', 'Researcher', 'Betelgeuse Seven']"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "personl = ['Ford Perfect','Male','Researcher','Betelgeuse Seven']\n",
    "personl"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5283e248",
   "metadata": {},
   "source": [
    "### 二.为列表增加标记（要记住索引位置上是什么）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "ee379c51",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Name',\n",
       " 'Huichun',\n",
       " 'Gender',\n",
       " 'Female',\n",
       " 'Occupation',\n",
       " 'Student',\n",
       " 'Home Planet',\n",
       " 'Guangdong Shantou']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person2 = ['Name','Huichun','Gender','Female','Occupation','Student','Home Planet','Guangdong Shantou']\n",
    "person2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0bdf6c07",
   "metadata": {},
   "source": [
    "# 五.购物车项目"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "7db61585",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------茶百道-------\n",
      "清喜葡萄冻冻 16元\n",
      "超级大瓜 20元\n",
      "豆乳玉麒麟 14元\n",
      "请输入您的余额(退出时输入Q)：16\n",
      "[['清喜葡萄冻冻', 16], ['超级大瓜', 20], ['豆乳玉麒麟', 14]]\n",
      "\n",
      "请输入商品编号进行商品的购买:2\n",
      "商品总价是20元\n",
      "余额不足，购买失败\n",
      "您购买的商品：['超级大瓜', 20]\n",
      "您还有-4元\n"
     ]
    }
   ],
   "source": [
    "goods_list = [[\"清喜葡萄冻冻\",16],[\"超级大瓜\",20],[\"豆乳玉麒麟\",14]]\n",
    "shopping_list = [\"1\",\"2\",\"3\"]\n",
    "goodsprice = [16,20,14]\n",
    "while True:\n",
    "    print(\"------茶百道-------\")\n",
    "    for i in range(3):\n",
    "        print(goods_list[i][0],str(goods_list[i][1])+\"元\")\n",
    "    balance = input(\"请输入您的余额\"'(退出时输入Q)：')\n",
    "    if balance == \"Q\":\n",
    "         break\n",
    "    print(f\"{goods_list}\\n\")\n",
    "    choose_goods = input(\"请输入商品编号进行商品的购买:\")\n",
    "    num = int( choose_goods)\n",
    "    goodschosen_price = goodsprice[num-1]\n",
    "    goodschosen_list = goods_list[num-1]\n",
    "    print(f\"商品总价是{goodschosen_price}\"+\"元\")\n",
    "    if int(balance) >= goodschosen_price:\n",
    "        print(\"购买成功\") \n",
    "    elif int(balance) < goodschosen_price:\n",
    "        print(\"余额不足，购买失败\")\n",
    "    balance = int(balance)\n",
    "    balance -= goodschosen_price\n",
    "    print(f\"您购买的商品：{goodschosen_list}\\n您还有{balance}\"+\"元\")\n",
    "    break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "559fdba0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
